<template>
  <div class="app-container">
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['boc:publish:add']"
        >新增资源上传</el-button>
      </el-col>
      <el-col :span="1.5" v-if="false">
        <el-button
            type="danger"
            plain
            icon="Delete"
            @click="handleDelete"
            v-hasPermi="['boc:publish:remove']"
        >删除资源</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            type="danger"
            plain
            icon="Delete"
            @click="testFunction"
        >测试方法</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getListFilesWithDates"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="publishList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="活动标题" align="center" prop="activityTitle" />
      <el-table-column label="活动描述" align="center" prop="activityDesc" />
      <el-table-column label="发布时间" align="center" prop="activityPtime">
        <template #default="scope">
          <span>{{ parseTime(scope.row.activityPtime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="有效开始时间" align="center" prop="activityStime">
        <template #default="scope">
          <span>{{ parseTime(scope.row.activityStime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="有效结束时间" align="center" prop="activityEtime">
        <template #default="scope">
          <span>{{ parseTime(scope.row.activityEtime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="活动积分" align="center" prop="activityIntegral" v-if="false"/>
      <el-table-column label="人数要求" align="center" prop="activityReqper" v-if="false"/>
      <el-table-column label="已报名人数" align="center" prop="activityRegper" v-if="false"/>
      <el-table-column label="活动发起人" align="center" prop="activityPubper" v-if="false"/>
      <el-table-column label="活动签到二维码" align="center" prop="activitySqrcode" width="100" v-if="false">
        <template #default="scope">
          <image-preview :src="scope.row.activitySqrcode" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="活动签退二维码" align="center" prop="activityEqrcode" width="100" v-if="false">
        <template #default="scope">
          <image-preview :src="scope.row.activityEqrcode" :width="50" :height="50"/>
        </template>
      </el-table-column>
    </el-table>

    <el-table v-loading="loading" :data="filesWithDatesList">
      <el-table-column label="文件名称" align="center" prop="fileName" />
      <el-table-column label="文件时间" align="center" prop="fileDate" />
    </el-table> 
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加资源 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="publishRef" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="资源附件" prop="docFiles">
          <boc-upload 
          v-model="form.docFiles"
          fileSize = 200
          :fileType = "fileType"
          />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="cancel">关闭</el-button>
        </div>
      </template>
    </el-dialog>
    
    <!-- 下载验证码输入 -->
    <el-dialog title="输入下载验证码" v-model="openPw" width="400px" append-to-body>
      <VerifyCodeInput @validate-result="handleValidate"/>
    </el-dialog>
    
  </div>
</template>

<script setup name="Publish">
import { listPublish, getPublish, delPublish, addPublish, updatePublish } from "@/api/boc/publish";
import { findRptIsAuth, listFilesWithDates } from "@/api/boc/common";
import { useRouter } from 'vue-router';



const { proxy } = getCurrentInstance();

const publishList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const total = ref(0);
const title = ref("");
const pDateRange = ref([]);
const validDateRange = ref([]);
const fileType = ref(["zip","jar"]);
const activeName = ref("");
const openPw = ref(false);

const filesWithDatesList = ref([]);

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
  },
  
});

const { queryParams, form, rules } = toRefs(data);


/** 查询活动发布列表 */
function getList() {
  loading.value = true;
  let search = queryParams.value;
  search.params = typeof (search.params) === 'object' && search.params !== null && !Array.isArray(search.params) ? search.params : {};
  search.params['pStartDate'] = pDateRange.value[0];
  search.params['pEndDate'] = pDateRange.value[1]; 
  let validStartDate = validDateRange.value[0];
  let validEndDate = validDateRange.value[1];

  if(validStartDate != null && validStartDate !== undefined)
  {
    search.params['validStartDate'] = validDateRange.value[0].slice(0,10)+" 23:59:59";
    search.params['validEndDate'] = validDateRange.value[1].slice(0,10)+" 00:00:00";
  }

  
  listPublish(search).then(response => {
    publishList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}


/** 新增按钮操作 */
function handleAdd() {
  open.value = true;
  title.value = "添加资源发布";
}

/** 删除按钮操作 */
function handleDelete(row) {
  proxy.$modal.confirm('是否确认删除已上传的资源项').then(function() {
    return delPublish();
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

/** 取消按钮 */
function cancel() {
  open.value = false;
  reset();
};

/**下载判断及处理开始 */
const router = useRouter();
function testFunction(){
  let rptPath = router.currentRoute.value.name;
  if(rptPath.includes("Rpt")){
    findRptIsAuth(rptPath).then(response => {
      let msg = response.msg;
      if(msg == "yes"){
        openPw.value = true;
      }else{
        proxy.$modal.msgError("没有下载权限,请先进行申请");
      }
    });
  }
}

function handleValidate(msg){
  openPw.value = false;
  if(msg == "yes"){
    alert("执行下载操作步骤");
  }else{
    proxy.$modal.msgError("验证不通过");
  }
}

/**下载判断及处理结束 */
function getListFilesWithDates(){
  listFilesWithDates(queryParams).then(response => {
    filesWithDatesList.value = response.data;
    total.value = 10;
    loading.value = false;
  });
}
getListFilesWithDates();
getList();
</script>
